from wsgiappclient.utils import get_session
from wsgiappclient.models.user import User

from paste.request import parse_formvars

class BaseController(object):

    def setup(self, environ, start_response):
        self.sess = get_session(environ)
        self.user = User(self.sess['user_id'])
        self.args = environ['wsgiorg.routing_args'][1]
        self.post = parse_formvars(environ)
        
    def dispatch(self, environ, start_response, key):
        method = environ['REQUEST_METHOD']
        args = environ.get('wsgiorg.routing_args', ([], {}))
        args = args[1]
        if args.get(key):
            meth = '_'.join([method, args[key]])
            print "Trying: %s" % meth
            if hasattr(self, meth):
                return getattr(self, meth)
        else:
            meth = '_'.join([method, 'index'])
            print "Trying: %s" % meth
            if hasattr(self, meth):
                return getattr(self, meth)
            
